
#include "../ui.h"
#include "../app_includes.h"

#define SETTING_TAB_NUM      (1)

static uint8_t tab_id = 0;
static lv_obj_t *ui_tabview;
static lv_obj_t *ui_tab[SETTING_TAB_NUM];
static void setting_tab0_screen_create(lv_obj_t *tab);
static void read_para(void);
static void write_para(lv_event_t * e);

void lv_setting_close(void)
{
    lv_status_bar_hidden(0);
    board_memory_write(DEVICE_STORE_ADDR, (uint8_t *)&app_dev.para.store, sizeof(app_dev.para.store));
}

//app应用入口
lv_obj_t *lv_setting_open(lv_obj_t *screen)
{
    ui_tabview = lv_tab_screen_create(screen, ui_tab, SETTING_TAB_NUM);
    setting_tab0_screen_create(ui_tab[0]);
    setting_group_select(0);
    lv_status_bar_hidden(1);
    return ui_tabview;
}

static lv_obj_t *add_panel(lv_obj_t *tab, char *name, lv_coord_t height)
{
    lv_obj_t *panel, *label;
    panel = lv_obj_create(tab);
    lv_obj_set_size(panel,  lv_pct(100), height);
    lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE);
    label = lv_label_create(panel);
    lv_label_set_text(label,name);
    lv_obj_set_style_text_font(label, &ui_font_FontChineseSmall, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_align( label, LV_ALIGN_LEFT_MID, 0, 0 );
    return panel;
}

static lv_obj_t *ui_sw_pc_comm;
static lv_obj_t *ui_sw_pd_open_auto;
static lv_obj_t *ui_sw_pd_close_auto;
static lv_obj_t *ui_edit_sleep_time;
static lv_obj_t *ui_silder_lcd_light;
static lv_obj_t *ui_silder_buzzer_vol;
static lv_obj_t *ui_dropdown_dir;

void setting_tab0_screen_create(lv_obj_t *tab)
{
    lv_obj_t *panel;
    lv_obj_set_flex_flow(tab,LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(tab, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
    //lv_obj_add_flag( tab, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    //lv_obj_set_scrollbar_mode(tab, LV_SCROLLBAR_MODE_AUTO);
    lv_obj_set_style_pad_row(tab, 2, LV_PART_MAIN| LV_STATE_DEFAULT);

    //pc通讯开关
    panel = add_panel(tab, "PC通讯", 30);
    ui_sw_pc_comm = lv_switch_create(panel);
    lv_obj_set_size(ui_sw_pc_comm, 50, 20);
    lv_obj_align( ui_sw_pc_comm, LV_ALIGN_RIGHT_MID, 0, 0 );
    //进入数控PD启动
    panel = add_panel(tab, "进入数控PD启动", 30);
    ui_sw_pd_open_auto = lv_switch_create(panel);
    lv_obj_set_size(ui_sw_pd_open_auto, 50, 20);
    lv_obj_align( ui_sw_pd_open_auto, LV_ALIGN_RIGHT_MID, 0, 0 );
    //退出数控PD关闭
    panel = add_panel(tab, "退出数控PD关闭", 30);
    ui_sw_pd_close_auto = lv_switch_create(panel);
    lv_obj_set_size(ui_sw_pd_close_auto, 50, 20);
    lv_obj_align( ui_sw_pd_close_auto, LV_ALIGN_RIGHT_MID, 0, 0 );
    //息屏
    panel = add_panel(tab, "息屏[分]", 30);
    ui_edit_sleep_time = lv_spinbox_create(panel);
    lv_obj_set_size(ui_edit_sleep_time, 50, 50);
    lv_obj_set_style_border_width(ui_edit_sleep_time, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_align( ui_edit_sleep_time, LV_ALIGN_RIGHT_MID, 0, 10 );
    lv_spinbox_set_digit_format( ui_edit_sleep_time, 2, 0);
    lv_spinbox_set_range( ui_edit_sleep_time, 1, 60 );
    lv_spinbox_set_step( ui_edit_sleep_time, 1);
    //屏幕亮度
    panel = add_panel(tab, "屏幕亮度", 30);
    ui_silder_lcd_light = lv_slider_create(panel);
    lv_slider_set_range(ui_silder_lcd_light, 1,10);
    lv_obj_set_width( ui_silder_lcd_light, 80);
    lv_obj_set_height( ui_silder_lcd_light, 10);
    lv_obj_align( ui_silder_lcd_light, LV_ALIGN_RIGHT_MID, 0, 0 );
    //蜂鸣器音量
    panel = add_panel(tab, "蜂鸣器音量", 30);
    ui_silder_buzzer_vol = lv_slider_create(panel);
    lv_slider_set_range(ui_silder_buzzer_vol, 0,10);
    lv_obj_set_width( ui_silder_buzzer_vol, 80);
    lv_obj_set_height( ui_silder_buzzer_vol, 10);
    lv_obj_align( ui_silder_buzzer_vol, LV_ALIGN_RIGHT_MID, 0, 0 );
    //显示方向
    panel = add_panel(tab, "显示方向", 30);
    ui_dropdown_dir = lv_dropdown_create(panel);
    lv_dropdown_set_options( ui_dropdown_dir, "0°\n90°\n180°\n270°" );
    lv_obj_set_width( ui_dropdown_dir, 80);
    lv_obj_set_height( ui_dropdown_dir, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_dropdown_dir, LV_ALIGN_RIGHT_MID );
    lv_obj_add_flag( ui_dropdown_dir, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    
    read_para();
    
    lv_obj_add_event_cb(ui_sw_pc_comm, write_para, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_sw_pd_open_auto, write_para, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_sw_pd_close_auto, write_para, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_edit_sleep_time, write_para, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_silder_lcd_light, write_para, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_silder_buzzer_vol, write_para, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ui_dropdown_dir, write_para, LV_EVENT_VALUE_CHANGED, NULL);
}

void setting_group_select(uint8_t id)
{
    if(id>=SETTING_TAB_NUM)
        return;
    lv_group_remove_all_objs(g_encoder);
    switch(id){
    case 0:  
        lv_group_add_obj(g_encoder, ui_sw_pc_comm);
        lv_group_add_obj(g_encoder, ui_sw_pd_open_auto);
        lv_group_add_obj(g_encoder, ui_sw_pd_close_auto);
        lv_group_add_obj(g_encoder, ui_edit_sleep_time);
        lv_group_add_obj(g_encoder, ui_silder_lcd_light);
        lv_group_add_obj(g_encoder, ui_silder_buzzer_vol);
        lv_group_add_obj(g_encoder, ui_dropdown_dir);
        break;
    }
}
static lv_obj_t *ui_sw_pc_comm;
static lv_obj_t *ui_sw_pd_open_auto;
static lv_obj_t *ui_sw_pd_close_auto;
static lv_obj_t *ui_edit_sleep_time;
static lv_obj_t *ui_silder_lcd_light;
static lv_obj_t *ui_dropdown_dir;

static void read_para(void)
{
    if(app_dev.para.store.pc_comm)
        lv_obj_add_state(ui_sw_pc_comm, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(ui_sw_pc_comm, LV_STATE_CHECKED);
    
    if(app_dev.para.store.pd_open_auto)
        lv_obj_add_state(ui_sw_pd_open_auto, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(ui_sw_pd_open_auto, LV_STATE_CHECKED);
        
    if(app_dev.para.store.pd_close_auto)
        lv_obj_add_state(ui_sw_pd_close_auto, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(ui_sw_pd_close_auto, LV_STATE_CHECKED);
    
    lv_spinbox_set_value( ui_edit_sleep_time, app_dev.para.store.lcd_sleep_time );
    lv_slider_set_value( ui_silder_lcd_light, app_dev.para.store.lcd_light/10, LV_ANIM_OFF);
    lv_slider_set_value( ui_silder_buzzer_vol, app_dev.para.store.buzzer_vol, LV_ANIM_OFF);
    lv_dropdown_set_selected(ui_dropdown_dir, app_dev.para.store.display_dir);
}

static void write_para(lv_event_t * e)
{
    app_dev.para.store.pc_comm = (lv_obj_get_state(ui_sw_pc_comm)&LV_STATE_CHECKED)==LV_STATE_CHECKED;
    app_dev.para.store.pd_open_auto = (lv_obj_get_state(ui_sw_pd_open_auto)&LV_STATE_CHECKED)==LV_STATE_CHECKED;
    app_dev.para.store.pd_close_auto = (lv_obj_get_state(ui_sw_pd_close_auto)&LV_STATE_CHECKED)==LV_STATE_CHECKED;
    app_dev.para.store.lcd_sleep_time = lv_spinbox_get_value(ui_edit_sleep_time);
    app_dev.para.store.lcd_light = lv_slider_get_value(ui_silder_lcd_light)*10;
    app_dev.para.store.buzzer_vol = lv_slider_get_value(ui_silder_buzzer_vol);
    app_dev.para.store.display_dir = lv_dropdown_get_selected(ui_dropdown_dir);
    
    app_dev_para_init();
    lv_obj_invalidate(ui_main_screen.screen);
}
